package cn.swu.lzr.Safe;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class Exit extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        if (session!=null)
        {
            Integer onlineCount = (Integer) getServletContext().getAttribute("onlineCount");

            // 如果在线人数计数器存在，则减少在线人数
            if (onlineCount != null && onlineCount > 0) {
                onlineCount--;
                getServletContext().setAttribute("onlineCount", onlineCount);
            }

            session.invalidate();
        }
        response.sendRedirect("index.html");

    }
}
